package validator;

import exception.ParamCheckFailureException;

/**
 * 数值校验器（不区分整型和浮点型）抽象层
 *
 * @Author 小白龙
 * @Date 2021-01-05
 * @Version 1.0
 */
public abstract class AbstractNumberValidator<V> extends AbstractValidator<INumberValidate, V> implements INumberValidate {

    public static final String ERROR_MESSAGE_MUST_GREAT_ZERO = "必须大于0";
    public static final String ERROR_MESSAGE_MUST_GREAT_EQUAL_ZERO = "必须大于等于0";
    public static final String ERROR_MESSAGE_MUST_LESS_ZERO = "必须小于0";
    public static final String ERROR_MESSAGE_MUST_LESS_EQUAL_ZERO = "必须小于等于0";
    public static final String ERROR_MESSAGE_NUM_NOT_MIN = "不能小于";
    public static final String ERROR_MESSAGE_NUM_NOT_MAX = "不能大于";

    @Override
    public final INumberValidate max(int max, String errorMessage) throws ParamCheckFailureException {
        return max(Long.valueOf(max), errorMessage);
    }

    @Override
    public final INumberValidate max(Long max) throws ParamCheckFailureException {
        return max(max, strConcat(getParamName(), ERROR_MESSAGE_NUM_NOT_MAX, String.valueOf(max)));
    }

    @Override
    public final INumberValidate min(int min, String errorMessage) throws ParamCheckFailureException {
        return min(Long.valueOf(min), errorMessage);
    }

    @Override
    public final INumberValidate min(Long min) throws ParamCheckFailureException {
        return min(min, strConcat(getParamName(), ERROR_MESSAGE_NUM_NOT_MIN, String.valueOf(min)));
    }

    // ---------------------------------------------------------------------- 范围校验 start
    @Override
    public final INumberValidate range(int min, int max, String error) throws ParamCheckFailureException {
        try {
            this.min(min);
            this.max(max);
        } catch (ParamCheckFailureException pcf) {
            throw createPcfException(error);
        }
        return this;
    }

    @Override
    public final INumberValidate range(int min, int max) throws ParamCheckFailureException {
        String errorStr = String.format("范围为：[%s, %s]", min, max);
        return this.range(min, max, strConcat(getParamName(), errorStr));
    }

    // ---------------------------------------------------------------------- 范围校验 end


    // --------------------------------------------------------------- 正负数校验 start
    @Override
    public final INumberValidate gtZero() throws ParamCheckFailureException {
        return gtZero(getParamName() + ERROR_MESSAGE_MUST_GREAT_ZERO);
    }

    @Override
    public final INumberValidate gtOrEqZero() throws ParamCheckFailureException {
        return gtOrEqZero(getParamName() + ERROR_MESSAGE_MUST_GREAT_EQUAL_ZERO);
    }


    @Override
    public final INumberValidate ltZero() throws ParamCheckFailureException {
        return ltZero(getParamName() + ERROR_MESSAGE_MUST_LESS_ZERO);
    }


    @Override
    public final INumberValidate ltOrEqZero() throws ParamCheckFailureException {
        return ltOrEqZero(getParamName() + ERROR_MESSAGE_MUST_LESS_EQUAL_ZERO);
    }
    // --------------------------------------------------------------- 正负数校验 end
}
